//
//  YHYActivityMessageViewController.m
//  Home
//
//  Created by 杨泓晔 on 16/6/22.
//  Copyright © 2016年 LanJue. All rights reserved.
//

#import "YHYActivityMessageViewController.h"
#import "WSHeaderActView.h"
@interface YHYActivityMessageViewController ()<UITextFieldDelegate,WSHeaderActViewDelegate,UIImagePickerControllerDelegate>{
    NSMutableArray *pwdIndicatorArr;
}
@property (strong, nonatomic) IBOutlet UIImageView *HeadImageView;
@property (strong, nonatomic) IBOutlet UITextField *companyTextField;
@property (strong, nonatomic) IBOutlet UITextField *rangeTextField;
@property (strong, nonatomic) IBOutlet UITextField *httpTextField;
@property (strong, nonatomic) IBOutlet UIButton *saveButton;

@property (nonatomic,strong)WSHeaderActView *headerAV;
@property (nonatomic,strong) UILabel *logoL;
@property (nonatomic, strong) UIView *shareBackGroundView;

@property (nonatomic,assign)BOOL hidden;


//@property (nonatomic, strong) UITextField *pwdTextField;
@property (nonatomic, strong)NSString *pwdTextField;
@property (nonatomic, assign) NSInteger numOfContent;

@end

@implementation YHYActivityMessageViewController




-(void) viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden=NO;
    self.tabBarController.tabBar.hidden=YES;
    
}

-(void) viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBarHidden=NO;
    self.tabBarController.tabBar.hidden=NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = LV;
    self.title = @"基本信息";
    [self creatView];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFiledEditChanged:)
                                                 name:@"UITextFieldTextDidChangeNotification" object:_pwdTextField];
    
}
- (void)creatView{
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(Back:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];

    
    self.hidden = YES;
    
    
    _HeadImageView = [UIImageView new];
    _HeadImageView.backgroundColor = LV;
    //添加边框
    [self.view addSubview:_HeadImageView];
    CALayer * layer = [_HeadImageView layer];
    layer.borderColor = KH.CGColor;
    layer.borderWidth = 0.5f;
    [_HeadImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(177/3 * AUTO_HEIGHT);
        make.size.mas_equalTo(CGSizeMake(100 * AUTO_WIDTH, 100 * AUTO_HEIGHT));
        make.centerXWithinMargins.mas_equalTo(self.view);
    }];
    _HeadImageView.backgroundColor = [UIColor redColor];
    CALayer *la = [_HeadImageView layer];
    
    la.borderColor = [[UIColor colorWithWhite:0.667 alpha:0.840]CGColor];
    la.borderWidth = 0.7;
    _HeadImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapV = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(HeaVTap:)];
    [_HeadImageView addGestureRecognizer:tapV];
    
    
    
    
    
    _logoL = [UILabel new];
    [_HeadImageView addSubview:_logoL];
    [_logoL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(70/3 * AUTO_HEIGHT);
        make.left.mas_equalTo(_HeadImageView);
        make.width.mas_equalTo(_HeadImageView);
        make.bottom.mas_equalTo(_HeadImageView.mas_bottom);
        
    }];
    _logoL.backgroundColor = [UIColor lightGrayColor];
    
    _logoL.text = @"公司Logo";
    _logoL.textColor = [UIColor whiteColor];
    _logoL.textAlignment = NSTextAlignmentCenter;
    _logoL.font = [UIFont fontWithName:TFont size:TEXTLABELFONT];
    
    
    
    UILabel *company = [UILabel new];
    [self.view addSubview:company];
    [company mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_HeadImageView.mas_bottom).offset(40 * AUTO_HEIGHT);
        make.width.mas_equalTo(100 * AUTO_WIDTH);
        make.height.left.mas_equalTo(25 * AUTO_HEIGHT);
        
        
    }];
    company.text = @"公司简称";
    company.font = [UIFont fontWithName:@"SHUZI" size:LITTLETEXTFONT];
    company.textColor = ZI;
    
    
    
    //公司简介
    _companyTextField = [UITextField new];
    [self.view addSubview:_companyTextField];
    [_companyTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(company.mas_bottom).offset(10 * AUTO_HEIGHT);
        make.left.mas_equalTo(self.view);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(50 * AUTO_HEIGHT);
    }];
    _companyTextField.textColor =HS;
    _companyTextField.backgroundColor = [UIColor whiteColor];
    
    
    UILabel *rangeL = [UILabel new];
    [self.view addSubview:rangeL];
    [rangeL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_companyTextField.mas_bottom).offset(20 * AUTO_HEIGHT);
        make.left.mas_equalTo(company);
        make.width.mas_equalTo(company);
        make.height.mas_equalTo(company);
        
    }];
    rangeL.text = @"经营范围";
    rangeL.font = [UIFont fontWithName:@"SHUZI" size:LITTLETEXTFONT];
    rangeL.textColor = ZI;
    
    
    
    
    _rangeTextField = [UITextField new];
    [self.view addSubview:_rangeTextField];
    [_rangeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(rangeL.mas_bottom).offset(10 * AUTO_HEIGHT);
        make.left.mas_equalTo(self.view);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(50 * AUTO_HEIGHT);
    }];
    _rangeTextField.textColor= HS;
    _rangeTextField.backgroundColor = [UIColor whiteColor];
    
    
    
    UILabel *httpL = [UILabel new];
    [self.view addSubview:httpL];
    [httpL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_rangeTextField.mas_bottom).offset(20 * AUTO_HEIGHT);
        make.left.mas_equalTo(company);
       // make.width.mas_equalTo(company);
        make.height.mas_equalTo(company);

        
    }];
    httpL.text = @"公司官网（选填）";
    httpL.font = [UIFont fontWithName:@"SHUZI" size:LITTLETEXTFONT];
    httpL.textColor = ZI;
    
    
    
    _httpTextField = [UITextField new];
    [self.view addSubview:_httpTextField];
    [_httpTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(httpL.mas_bottom).offset(10 * AUTO_HEIGHT);
        make.left.mas_equalTo(self.view);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(50 * AUTO_HEIGHT);
    }];
    _httpTextField.backgroundColor = [UIColor whiteColor];
    _httpTextField.textColor = HS;

    
    
    
    
    
//    _companyTextField.text = _pwdTextField;
//
    
//    _numOfContent = 5;
    
    _companyTextField.tag = 1000;
    _rangeTextField.tag = 1001;
    _httpTextField.tag = 1002;
    
    
    
    
     _companyTextField.delegate = self;
    _rangeTextField.delegate = self;
    _httpTextField.delegate = self;
    
    
     _companyTextField.text = _pwdTextField;
    if (_companyTextField.tag == 1000) {
       
        _numOfContent = 9;
        
    }
    if (_rangeTextField.tag == 1001) {
        _numOfContent = 19;
       
    }
    if (_httpTextField.tag == 1002) {
        _numOfContent = 19;
         _httpTextField.keyboardType = UIKeyboardTypeURL;
    }
    
    
    
    
    
    _saveButton = [UIButton new];
    [self.view addSubview:_saveButton];
    //self.saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.saveButton.backgroundColor = GH;
    self.saveButton.layer.masksToBounds = YES;
    self.saveButton.layer.cornerRadius = 5;
    self.saveButton.tag=1001;
      [self.saveButton setTitle:@"保存" forState:UIControlStateNormal];
    [self.saveButton addTarget:self action:@selector(goNext:) forControlEvents:UIControlEventTouchUpInside];
    [self.saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    // [self.button setTitleColor:HS forState:UIControlStateNormal];
    //    self.button.frame = CGRectMake(50.00*AUTO_WIDTH, 600.00*AUTO_WIDTH, 275.00*AUTO_WIDTH, 40.00*AUTO_WIDTH);
    [self.saveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_httpTextField.mas_bottom).offset(25 * AUTO_HEIGHT);
        make.left.mas_equalTo(30 * AUTO_WIDTH);
        make.right.mas_equalTo(-30 * AUTO_WIDTH);
        make.height.mas_equalTo(40 * AUTO_HEIGHT);
        
    }];
    self.saveButton.titleLabel.font = [UIFont fontWithName:@"Arial" size:TEXTCONTENTFONT * TEXTWEIGHT];
    

    
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(TapAction:)];
    [self.view addGestureRecognizer:tap];
}

- (void)TapAction:(UITapGestureRecognizer *)tap{
//    [commentText resignFirstResponder];

    
    [self.companyTextField resignFirstResponder];
    [self.rangeTextField resignFirstResponder];
    [self.httpTextField resignFirstResponder];
    
    [UIView animateWithDuration:0.20f animations:^{
        
        
        self.view.frame =CGRectMake(0, 0 * AUTO_HEIGHT,SCREEN_WIDTH,SCREEN_HEIGHT);
        [_HeadImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(177/3* AUTO_HEIGHT + 64 * AUTO_HEIGHT);
            make.size.mas_equalTo(CGSizeMake(100 * AUTO_WIDTH, 100 * AUTO_HEIGHT));
            make.centerXWithinMargins.mas_equalTo(self.view);
        }];
        
    }];


}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    
    [UIView animateWithDuration:0.20f animations:^{
        
        
        self.view.frame =CGRectMake(0, 0 * AUTO_HEIGHT,SCREEN_WIDTH,SCREEN_HEIGHT);
        [_HeadImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(177/3* AUTO_HEIGHT + 64 * AUTO_HEIGHT);
            make.size.mas_equalTo(CGSizeMake(100 * AUTO_WIDTH, 100 * AUTO_HEIGHT));
            make.centerXWithinMargins.mas_equalTo(self.view);
        }];
        
    }];
    

    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField.text == self.pwdTextField) {
        if (self.pwdTextField.length > _numOfContent) {
            
            if (textField.text.length > _numOfContent){
                
                return NO;
            }
        }
        
    }
    
    return YES;
}

- (void)setDotWithCount:(NSInteger)count {
    for (UILabel *dot in pwdIndicatorArr) {
        dot.hidden = YES;
    }
    
    for (int i = 0; i< count; i++) {
        ((UILabel*)[pwdIndicatorArr objectAtIndex:i]).hidden = NO;
    }
}




-(void) textFieldDidChanged:(UITextField *) textField
{
    NSString *toBeString=textField.text;
    
    //获取高亮部分
    UITextRange *selectedRange=[textField markedTextRange];
    UITextPosition  *position=[textField positionFromPosition:selectedRange.start offset:0];
    
    //没有高亮选择的字，则对已输入的文字进行字数统计和限制
    if (!position) {
        
        //编写正则表达式：只能是数字或英文，或两者都存在
        NSString *regex=@"^[\u4E00-\u9FA5]+$";
        
        //创建谓词对象设定条件的表达式
        NSPredicate *predicate=[NSPredicate predicateWithFormat:@"SELF MATCHES%@",regex];
        
        NSMutableString *muStr=[[NSMutableString alloc]initWithString:toBeString];
        NSMutableArray *ranges=[[NSMutableArray alloc]init];
        for (int i=0; i<toBeString.length; i++) {
            
            NSString *str=[toBeString substringWithRange:NSMakeRange(i, 1)];
            
            //对字符串进行判断
            if (![predicate evaluateWithObject:str]) {
                
                if (![ranges containsObject:str]) {
                    [ranges addObject:str];
                }
            }
        }
        for (NSString *string in ranges) {
            [muStr  replaceOccurrencesOfString:string withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, muStr.length)];
        }
        if (muStr.length >10) {
            
            NSRange rangelndex=[muStr rangeOfComposedCharacterSequenceAtIndex:10];
            
            if (rangelndex.length ==1) {
                textField.text=[muStr substringToIndex:10];
            }else{
                
                NSRange  rangeRange=[muStr rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, 10)];
                textField.text=[muStr substringWithRange:rangeRange];
            }
        }
        else{
            
            textField.text=muStr;
        }
    }
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    [UIView animateWithDuration:.7f delay:0.f usingSpringWithDamping:.7f initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
    } completion:nil];
    
    if (textField.tag == 1000) {
//        [UIView animateWithDuration:.3f animations:^{
//            self.view.frame = CGRectMake(0, -  * AUTO_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
//            
//        }];
    }
    if (textField.tag == 1001) {
        [UIView animateWithDuration:.3f animations:^{
            self.view.frame = CGRectMake(0, - 107 * AUTO_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
            
        }];

    }
    if (textField.tag == 1002) {
        [UIView animateWithDuration:.3f animations:^{
            self.view.frame = CGRectMake(0, - 167 * AUTO_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT);
            
        }];
        
    }
    
    return YES;
}

#pragma mark - Notification Method
-(void)textFiledEditChanged:(NSNotification *)obj
{
    UITextField *textField = (UITextField *)obj.object;
    NSString *toBeString = textField.text;
    NSString *lang = [textField.textInputMode primaryLanguage];
    if ([lang isEqualToString:@"zh-Hans"])// 简体中文输入
    {
        //获取高亮部分
        UITextRange *selectedRange = [textField markedTextRange];
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position)
        {
            if (toBeString.length > _numOfContent)
            {
                textField.text = [toBeString substringToIndex:_numOfContent];
            }
        }
        
    }
    // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
    else
    {
        if (toBeString.length > _numOfContent)
        {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:_numOfContent];
            if (rangeIndex.length == 1)
            {
                textField.text = [toBeString substringToIndex:_numOfContent];
            }
            else
            {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, _numOfContent)];
                textField.text = [toBeString substringWithRange:rangeRange];
            }
        }
    }
}


//把回车键当做退出键盘的响应键  textView退出键盘的操作

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

{
    
    
    
    if ([@"\n" isEqualToString:text] == YES)
        
    {
        
        [textView resignFirstResponder];
        
        
        
        
        
        return NO;
        
    }
    
    
    
    return YES;
    
}


- (void)HeaVTap:(UITapGestureRecognizer *)tap{
    [self show];
}


#pragma mark - 显示动画
- (void)show {
    
    self.shareBackGroundView.hidden = NO;
    self.hidden = NO;
    [UIView animateWithDuration:0.3 animations:^{
        
        self.shareBackGroundView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
        //self.view.frame = CGRectMake(0, SCREEN_HEIGHT - (160 * AUTO_WIDTH) - 64, SCREEN_WIDTH, 160 * AUTO_WIDTH);
    }];
}

#pragma mark - 头像点击View
- (WSHeaderActView *)headerAV {
    
    if (!_headerAV) {
        self.headerAV = [[WSHeaderActView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        self.headerAV.delegate = self;
        [self.view addSubview:_headerAV];
    }
    
    return _headerAV;

}
#pragma mark - headerAV 点击事件代理
- (void)presentImagePicker:(OPENIMAGE)img {
    
    if (img == 0) {
        [self selectCamera];
    } else if (img == 1) {
        [self selectPics];
    } else {
        
    }
}

- (void)selectCamera {
    
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    if (![UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
        sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];//初始化
    picker.delegate = self;
    picker.allowsEditing = YES;//设置可编辑
    picker.sourceType = sourceType;
    [self presentViewController:picker animated:YES completion:nil];//进入照相界面
}

- (void)selectPics {
    
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.delegate = self;
    imagePicker.allowsEditing = YES;
    [self presentViewController:imagePicker animated:YES completion:nil];
}


- (void)hiden {
    
    self.hidden = YES;
    [UIView animateWithDuration:0.3 animations:^{
        
      //  self.backgView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 160 * AUTO_WIDTH);
        self.shareBackGroundView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
    } completion:^(BOOL finished) {
        
        self.shareBackGroundView.hidden = YES;
    }];
}

// 选中照片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    
    _HeadImageView = [info objectForKey:UIImagePickerControllerEditedImage];
    [picker dismissViewControllerAnimated:YES completion:NULL];
  //  [self.myTableView reloadData];
    
}



//返回按钮
-(void) Back:(UIButton *) btn
{
    [self.navigationController popViewControllerAnimated:YES];
}





- (void)goNext:(UIButton *)btu{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
